package com.dongnam.quanly.common.controller;

import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.dao.PhongBanDao;
import com.dongnam.quanly.common.exceptions.DatabaseException;
import com.dongnam.quanly.common.model.NhomDoiTac;
import com.dongnam.quanly.common.model.PhongBan;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class PhongBanController extends GenericController<PhongBan, String> {

	private static final Logger logger = LoggerFactory.getLogger(PhongBanController.class);

	public PhongBanController() {
		super(new PhongBanDao());
		// TODO Auto-generated constructor stub
	}

	@Override
	public PhongBanDao getDao() {
		// TODO Auto-generated method stub
		return (PhongBanDao) dao;
	}

	public boolean checkIsHasChild(String maphongban) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		boolean flag = true;
		try {
			PhongBan pb = findById(maphongban, false);
			flag = pb.getIsHasChild();
			if (!isTransacted) {
				session.getTransaction().commit();
			}

			return flag;
		} catch (Exception e) {
			rollbackConditionally(isTransacted, session);
			throw new DatabaseException(e);
		}
	}

	public List<PhongBan> listPhongBanExclude(List<String> listPhongBan) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<PhongBan> list = getDao().listPhongBanExclude(listPhongBan);

			if (!isTransacted) {
				session.getTransaction().commit();
			}

			return list;
		} catch (Exception e) {
			rollbackConditionally(isTransacted, session);
			throw new DatabaseException(e);
		}

	}

	public List<PhongBan> listPhongBanInclude(List<String> listPhongBan) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<PhongBan> list = getDao().listPhongBanInclude(listPhongBan);

			if (!isTransacted) {
				session.getTransaction().commit();
			}

			return list;
		} catch (Exception e) {
			rollbackConditionally(isTransacted, session);
			throw new DatabaseException(e);
		}

	}

}
